from PyQt5.QtWidgets import QDialog, QPushButton, QLabel, QHBoxLayout, QVBoxLayout, QGridLayout, QLineEdit
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtCore import pyqtSlot, Qt
# 导入自己定义的FileOperate模块
from FileOperate import ExcelOperate

class Dialog(QDialog):
    def __init__(self, identity):
        super().__init__()
        self.setup_ui(identity)

    def setup_ui(self, identity):
        self.move(650, 350)

        self.__identity = identity
        self.__label_id = QLabel("工号")
        self.__input_id = QLineEdit()
        self.__label_pass = QLabel("密码")
        self.__input_pass = QLineEdit()
        # 设置密码输入为密文
        self.__input_pass.setEchoMode(QLineEdit.Password)

        grid_lay_out = QGridLayout()
        grid_lay_out.addWidget(self.__label_id, 1, 0)
        grid_lay_out.addWidget(self.__input_id, 1, 1)
        grid_lay_out.addWidget(self.__label_pass, 2, 0)
        grid_lay_out.addWidget(self.__input_pass, 2, 1)

        self.__button_ok = QPushButton("确认")
        # 按钮连接信号槽函数
        self.__button_ok.clicked.connect(self.on_click_button_ok)

        hbox = QHBoxLayout()
        hbox.addWidget((self.__button_ok))

        vbox = QVBoxLayout()
        vbox.addLayout(grid_lay_out)
        vbox.addLayout(hbox)

        self.setLayout(vbox)

        if identity == 0:
            self.setWindowTitle("学生系统")
        else:
            self.setWindowTitle("教师系统")
    
    """
    status: 作为是否登录成功的状态变量
    """
    def get_status(self):
        return self.__status

    """
    返回学生的账号
    """
    def get_stu_id(self):
        return self.__id

    @pyqtSlot()
    def on_click_button_ok(self):
        self.__id = self.__input_id.text()
        self.__password = self.__input_pass.text()
        # 系统中只有一个管理员，工号为0，密码为0
        if self.__identity == 1:
            if self.__id == '0' and self.__password == '0':
                self.__status = 0
                
            else:
                self.__status = 1
        else:
            # 获取输入的账号和密码
            excel_operate_obj = ExcelOperate("./stuLists.xlsx")
            res = excel_operate_obj.check_password("stuLists", self.__id, self.__password)
            if res:
                self.__status = 0
            else:
                self.__status = 1

        self.accept()